package com.ars.common.servlet.admin.material;

import com.ars.common.beans.Active.Mysubmit;
import com.ars.common.beans.User.User;
import com.ars.common.dao.activity.ClassActiveMaterialMysql;
import com.ars.common.dao.activity.FileToZip;
import com.ars.common.dao.activity.FindActivityById;
import com.ars.common.beans.Active.Active;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/ThroughOutActiveServlet")
public class ThroughOutActiveServlet extends HttpServlet {

    private static final String JSON_RESPONSE_TYPE = "json";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User logUser = (User) request.getSession().getAttribute("LogUser");
        String UserID = logUser.getUserID();
        String id = request.getParameter("id");

        ArrayList<Mysubmit> mysubmits = new ArrayList<>();

        try {
            mysubmits =  ClassActiveMaterialMysql.findMysubmitList(UserID, Integer.parseInt(id));
            request.setAttribute("ClassSubmit", mysubmits);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        /**************************************************/

        System.out.println("id:" + id);

        String responseType = request.getParameter("responseType");

        if (id == null || id.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("{\"error\":\"Activity ID is required.\"}");
            return;
        }

        Active active;
        try {
            active = FindActivityById.findActivityById(id);
        } catch (SQLException | ClassNotFoundException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("{\"error\":\"Error retrieving activity.\"}");
            return;
        }

        if (JSON_RESPONSE_TYPE.equalsIgnoreCase(responseType)) {
            respondWithJson(response, active);
        } else {
            forwardToJsp(request, response, active);
//            respondWithJson(response, active);
        }
    }

    private void respondWithJson(HttpServletResponse response, Active active) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String jsonResponse = mapper.writeValueAsString(active);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonResponse);
        System.out.println("jsonResponse:" + jsonResponse);
    }

    private void forwardToJsp(HttpServletRequest request, HttpServletResponse response, Active active) throws ServletException, IOException {
        request.setAttribute("active", active);
        request.getRequestDispatcher("/admin/throughOutActive.jsp").forward(request, response);
    }
}
